package com.neuedu.mvc.controller;

import com.neuedu.dao.UserDao;
import com.neuedu.dao.UserDao;
import com.neuedu.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;

@WebServlet(urlPatterns = "/user")
public class UserController extends HttpServlet {

    public static final String LIST = "list";
    public static final String SAVE = "save";
    public static final String TO_EDIT = "toEdit";
    public static final String UPDATE = "update";
    public static final String DELETE_BY_ID = "deleteById";
    private UserDao userDao = new UserDao();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //根据 条件 执行不同的功能
//        _type

        String type = request.getParameter("_type");
        if(type == null || LIST.equals(type)){
            this.queryList(request,response);
        }else if (SAVE.equals(type)){
            this.save(request,response);
        }else if (TO_EDIT.equals(type)){
            this.toEdit(request,response);
        }else if(UPDATE.equals(type)){
            this.update(request,response);
        }else if(DELETE_BY_ID.equals(type)){
            this.deleteById(request,response);
        }



    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取主键
        String userid = request.getParameter("userid");

        int i = userDao.deleteById(Integer.parseInt(userid));

        response.sendRedirect(request.getContextPath() + "/user?_type=list");
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("userid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String expire = request.getParameter("expire");
        String nickname = request.getParameter("nickname");
        String lastlogin = request.getParameter("lastlogin");
        String sal = request.getParameter("sal");
        

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //2 User
        try {
            User user = new User(
                    Integer.parseInt(userid),
                    username,
                    password,
                    expire!=null?simpleDateFormat.parse(expire):null,
                    nickname,
                    lastlogin!=null?simpleDateFormat.parse(lastlogin):null,
                    sal!=null?Double.valueOf( sal):null
            );
            //3 save
            int insert = userDao.updateById(user);

            response.sendRedirect(request.getContextPath() + "/user?_type=list");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //查询一条记过
        String userid = request.getParameter("userid");
        User user = userDao.selectOne("where userid = ?", userid);

        //共享数据
        request.setAttribute("user",user);

        //内部跳转  user_edit.jsp  渲染显示
        request.getRequestDispatcher("/business/user/user_edit.jsp").forward(request,response);
    }

    /**
     * 添加员工
     * @param request
     * @param response
     */
    private void save(HttpServletRequest request, HttpServletResponse response) {
        String userid = request.getParameter("userid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String expire = request.getParameter("expire");
        String nickname = request.getParameter("nickname");
        String lastlogin = request.getParameter("lastlogin");
        String sal = request.getParameter("sal");


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //2 User
        try {
            User user = new User(
                    Integer.parseInt(userid),
                    username,
                    password,
                    expire!=null?simpleDateFormat.parse(expire):null,
                    nickname,
                    lastlogin!=null?simpleDateFormat.parse(lastlogin):null,
                    sal!=null?Double.valueOf( sal):null
            );


            int insert = userDao.insert(user);

            //2 重定向 告诉浏览器重新发起请求  第二次请求
            response.sendRedirect(request.getContextPath() + "/user?_type=list");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    private void queryList(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //调用
        String where = "";
        Object[] params = new Object[0];
        List<User> userList = userDao.selectList(where, params);


        //共享数据
        request.setAttribute("users",userList);
        //users 渲染 Table

        //内部跳转的 / 相对的当前项目
        request.getRequestDispatcher("/business/user/user_list.jsp").forward(request,response);

    }


}
